package com.ddc.drivers.fmpxml;

import java.sql.Types;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

public class FmFieldList {
	private List fields;
	public boolean wasNull;
	private LogMaker logMaker = new LogMaker();

	public FmFieldList() {
		this.fields = new ArrayList();
	}

	public void add(FmField field) {
		fields.add(field);
/*
//		SB MOD
		String tempSB = "";
		try{
			int type = (field.getType()).getSqlDataType();
			switch (type) {
				case Types.LONGVARCHAR:
					tempSB = "LONGVARCHAR " + type;
				break;
				case Types.VARCHAR:
					tempSB = "VARCHAR " + type;
				break;
				case Types.DOUBLE: tempSB = "DOUBLE " + type;
				break;
				case Types.DECIMAL: tempSB = "DECIMAL " + type;
				break;
				case Types.DATE: tempSB = "DATE " + type;
				break;
				case Types.TIME: tempSB = "TIME " + type;
				break;
				case Types.TIMESTAMP: tempSB = "TIMESTAMP " + type;
				break;
				case Types.BLOB: tempSB = "BLOB " + type;
				break;
				default: tempSB = "NA " + type;
			}
			logMaker.write("NEW FIELD: fieldName:" + field.getColumnName() + "; fieldTypeName:" + (field.getType()).getTypeName() + "; fieldsqlType:" + (field.getType()).getSqlDataType() + " | " + tempSB);
		}catch(Exception e){logMaker.write("FMFieldList add() Error:" + e);}
*/

	}

	public FmField get(int index) {
		return (FmField) fields.get(index);
	}

	/** Returns the FmField objects contained in this FmFieldList. */
	public List getFields() {
		return fields;
	}

	public int size() {
		return fields.size();
	}

	//OPTIMIZE This could be faster
	public int indexOfFieldWithAlias(String alias) {
		int i=0;
		for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
			FmField fmField = (FmField) iterator.next();
			if (fmField.getAlias().equalsIgnoreCase(alias)) return i;
			else i++;
		}
		//throw new IllegalArgumentException("No such field '" + alias + "'");
		return -1;
	}

	//FIX!! This is really, really slow - we should build a hashset of all of the fields instead.
	public int indexOfFieldWithColumnName(String columnName) {
		int i=0;
		for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
			FmField fmField = (FmField) iterator.next();
			if (fmField.getColumnName().equalsIgnoreCase(columnName)) return i;

			else i++;
		}
		//throw new IllegalArgumentException("No such field '" + columnName + "'");
		return -1;
	}

	public Iterator iterator() {
		return fields.iterator();
	}

	public String toString() {
		return fields.toString();
	}
}